<?php
/**
 * File containing the BRail_Teamware_UserQueryTool class.
 * @package BRail_Teamware
 */

/**
 * @package BRail_Teamware
 */
class BRail_Teamware_UserQueryTool
{
    /**
     * @var BRail_Teamware_Adapter $_twAdapter
     */
    private $_twAdapter = null;

    /**
     * @param BRail_Teamware_Adapter $twAdapter
     */
    public function __construct(BRail_Teamware_Adapter $twAdapter)
    {
        $this->_twAdapter = $twAdapter;
    }

    /**
     * @param string $type
     * @param string $pattern
     * @return array
     * @throws BRail_Teamware_Exception
     */
    private function customSearch($type, $pattern)
    {
        if (empty($type) || empty($pattern)) {
            throw new BRail_Teamware_Exception(
                __METHOD__.' expects a search type and a pattern');
        }

        $returnArray = array();

        $this->_twAdapter->makeCaseInsensitive();

        $results = $this->_twAdapter->execute(
            'SELECT ident, surname, givname, grade, oree, unor, teltw1, teltw2, e_mail, lan, ' .
            'section, exploit, category, address, unit ' .
            'FROM S972_TWA500_VW ' .
            'WHERE '. $type .' LIKE \''. $pattern . '\''
        );

        $nbEntries = count($results);
        for ($i = 0; $i < $nbEntries; $i++) {
            $returnArray[] = new BRail_Teamware_Entry(
                iconv("ISO-8859-1", "UTF-8", $results[$i]['IDENT']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['SURNAME']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['GIVNAME']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['GRADE']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['OREE']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['UNOR']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['TELTW1']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['TELTW2']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['E_MAIL']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['LAN']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['SECTION']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['EXPLOIT']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['CATEGORY']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['ADDRESS']),
                iconv("ISO-8859-1", "UTF-8", $results[$i]['UNIT'])
            );
        }

        return $returnArray;
    }

    /**
     * @param string $value
     * @return array
     */
    public function searchByName($value)
    {
        return $this->customSearch('SURNAME', $value);
    }

    /**
     * @param string $value
     * @return array
     */
    public function searchByFirstname($value)
    {
        return $this->customSearch('GIVNAME', $value);
    }

    /**
     * @param string $value
     * @return array
     */
    public function searchByIdent($value)
    {
        return $this->customSearch('IDENT', $value);
    }

    /**
     * @param string $value
     * @return array
     */
    public function searchByPhone($value)
    {
        return $this->customSearch('TELTW1', $value);
    }

    /**
     * @param string $value
     * @return array
     */
    public function searchByLogin($value)
    {
        return $this->customSearch('IDENT', self::getIdentByLogin($value) . '%');
    }

    /**
     * @param string $login
     * @return string
     */
    public static function getIdentByLogin($login)
    {
        return BRail_User::getIdentByLogin($login);
    }
}
